const vm = new Vue({
    el: '#main',
    data: {
        view: {
            end: false,
        },
        data: {
            going: [],
            end: [],
        },
        pageNum: 1,
    },
    methods: {
        authTrade,
        applyForTakePhoto,
        fixImgUrl,
        toggleEnd,
        deleteHouse,
        goMenu,
        goPage,
        onlineHouse,
        offlineHouse,
    },
});

function apiready() {
    fxj.fixLayout();
    reqOrderList(vm.pageNum);
    loadMore();
    if (!fxj.getSearch().get('back')) {
        const back = document.getElementById('backable');
        back.parentNode.removeChild(back);
    }
    setCustomRefreshHeaderInfo(() => {
        reqOrderList(1);
    });
}

function toggleEnd(b) {
    this.view.end = b;
}

function deleteHouse(id) {
    fxj.dialog.alert({
        msg: '确认删除房源?',
        btnLeft: '删除',
        btnRight: '取消',
        leftCallback: async() => {
            const close = fxj.dialog.loading();
            const res = await ajax({
                url: 'home/houseManage/houseDelete',
                data: {
                    houseId: id,
                },
            });
            close();
            toast(res.msg);
            if (res.err === 0) {
                reqOrderList(1);
            }
        },
    });
}

function goMenu(id) {
    goPage('hm_menu.html', {
        data: {
            houseId: id,
            type: 'alter',
        },
    });
}

async function onlineHouse(houseId) {
    const res = await ajax({
        url: 'home/houseManage/houseUp',
        data: {
            houseId,
        },
    });
    toast(res.msg, () => {
        reqOrderList(1);
    });
}

async function offlineHouse(houseId) {
    const res = await ajax({
        url: 'home/houseManage/houseDown',
        data: {
            houseId,
        },
    });
    toast(res.msg, () => {
        reqOrderList(1);
    });
}

async function reqOrderList(pageNum = vm.pageNum) {
    const res = await ajax({
        url: 'home/houseManage/getAllHouseList',
        data: {
            pageNum,
            role: 1,
        },
    });
    if (res.data.offLine.houseData) {
        if (pageNum === 1) {
            vm.data.going = res.data.onLine.houseData;
        } else {
            vm.data.going.push(...res.data.onLine.houseData);
        }
    }
    if (res.data.onLine.houseData) {
        if (pageNum === 1) {
            vm.data.end = res.data.offLine.houseData;
        } else {
            vm.data.end.push(...res.data.offLine.houseData);
        }
    }
    vm.pageNum += 1;
}

function fixImgUrl(url) {
    return fxj.fixImgUrl(url);
}

function loadMore() {
    isToBottom(100, (ok) => {
        if (!ok) return;
        vm.pageNum += 1;
        reqOrderList(vm.pageNum);
    });
}

function applyForTakePhoto(evt, type, houseId) {
    evt.stopPropagation();
    const data = {
        houseId,
    };
    if (type === '申请') {
        goPage('apply_take_photo.html', {
            data
        });
    } else if (type === '进度') {
        goPage('take_photo_progress.html', {
            data
        });
    }
}

function authTrade(evt) {
    evt.stopPropagation();
}